package com.zhangtai.modules.service.impl.employee;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangtai.modules.dao.EmployeeSecondmentDao;
import com.zhangtai.modules.entity.employee.EmployeeSecondmentEntity;
import com.zhangtai.modules.service.employee.EmployeeSecondmentService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class EmployeeSecondmentServiceImpl extends ServiceImpl<EmployeeSecondmentDao, EmployeeSecondmentEntity> implements EmployeeSecondmentService {

    @Override
    public PageResult queryPage(@RequestParam(value = "orgIds",required = false) List<Long> orgIds,
                                 @RequestParam(value = "name", required = false) String name,
                                @RequestParam(value = "status",required = false) List<Integer> status,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        QueryWrapper<EmployeeSecondmentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(wrapper -> wrapper.in(!CollectionUtils.isEmpty(orgIds),"unit_id", orgIds).or()
                .in(!CollectionUtils.isEmpty(orgIds),"secondment_unit_id", orgIds));

        queryWrapper.like(!StringUtils.isEmpty(name),"name",name)
                .eq(orgIds!=null&&orgIds.size()==0,"unit_id", null)
//                        .eq(orgIds!=null&&orgIds.size()==0,"secondment_unit_id", null)
                .in(!CollectionUtils.isEmpty(status),"status", status)
                .eq("is_delete",0).orderByDesc("create_time");

        IPage<EmployeeSecondmentEntity> page = this.page(
                new Page<EmployeeSecondmentEntity>(pageNum,pageSize),queryWrapper );

        return new PageResult(page);
    }

    @Override
    public EmployeeSecondmentEntity getByIdE(Long id) {
            EmployeeSecondmentEntity one = this.getOne(new QueryWrapper<EmployeeSecondmentEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(EmployeeSecondmentEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(EmployeeSecondmentEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            EmployeeSecondmentEntity entity = new EmployeeSecondmentEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}